﻿Public Class clsInhMaterial
    Inherits clsMaterial

#Region "Attributes"

    Private mvarGatunek As clsGatunek
    Private mvarDostawca As clsDostawca
    Private mvarStop As clsStop
    Private mvarSkaldChemiczny As Collection(Of clsSkladChemiczny)
    Private mvarSkaldChemicznyDel As Collection(Of clsSkladChemiczny)
    Private mvarWaluta As clsWaluta

#End Region

#Region "Properties"

    Public Property Gatunek() As clsGatunek
        Get
            Return mvarGatunek
        End Get
        Set(ByVal value As clsGatunek)
            mvarGatunek = value
        End Set
    End Property

    Public Property Dostawca() As clsDostawca
        Get
            Return mvarDostawca
        End Get
        Set(ByVal value As clsDostawca)
            mvarDostawca = value
        End Set
    End Property

    Public Property StopNazwa() As clsStop
        Get
            Return mvarStop
        End Get
        Set(ByVal value As clsStop)
            mvarStop = value
        End Set
    End Property

    Public Property SkladChemiczny() As Collection(Of clsSkladChemiczny)
        Get
            Return mvarSkaldChemiczny
        End Get
        Set(ByVal value As Collection(Of clsSkladChemiczny))
            mvarSkaldChemiczny = value
        End Set
    End Property

    Public Property SkladChemicznyDel() As Collection(Of clsSkladChemiczny)
        Get
            Return mvarSkaldChemicznyDel
        End Get
        Set(ByVal value As Collection(Of clsSkladChemiczny))
            mvarSkaldChemicznyDel = value
        End Set
    End Property

    Public Property Waluta() As clsWaluta
        Get
            Return mvarWaluta
        End Get
        Set(ByVal value As clsWaluta)
            mvarWaluta = value
        End Set
    End Property

#End Region

#Region "Methods"

    Public Sub New()
        MyBase.New()
        mvarGatunek = New clsGatunek()
        mvarDostawca = New clsDostawca()
        mvarStop = New clsStop()
        mvarSkaldChemiczny = New Collection(Of clsSkladChemiczny)
        mvarSkaldChemicznyDel = New Collection(Of clsSkladChemiczny)
        mvarWaluta = New clsWaluta
    End Sub

    Public Sub New(ByVal ID As Integer, ByVal lvarDbHolder As clsDataBase)
        MyBase.New(ID, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarGatunek = New clsGatunek(MyBase.GatunkeId, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarDostawca = New clsDostawca(MyBase.DostawcaId, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarWaluta = New clsWaluta(MyBase.WalutaId, lvarDbHolder, VAR_ERROR_HANDLER)

        mvarSkaldChemiczny = New Collection(Of clsSkladChemiczny)
        mvarSkaldChemicznyDel = New Collection(Of clsSkladChemiczny)
        Dim lvarTabel2 As DataTable = PobierzSkladChemiczny(lvarDbHolder, ID)

        If Not lvarTabel2 Is Nothing Then
            For Each lvarRow As DataRow In lvarTabel2.Rows
                mvarSkaldChemiczny.Add(New clsInhSkladChemiczny(CInt(lvarRow.Item("id")), lvarDbHolder))
            Next
        End If

    End Sub

    Public Overloads Sub Save(ByVal lvarDbHolder As clsDataBase)

        MyBase.Save(lvarDbHolder, VAR_ERROR_HANDLER)

        For Each lvarPozycja As clsInhSkladChemiczny In mvarSkaldChemicznyDel
            lvarPozycja.Delete(lvarDbHolder)
        Next
        For Each lvarPozycja As clsInhSkladChemiczny In mvarSkaldChemiczny
            lvarPozycja.MaterialId = MyBase.Id
            lvarPozycja.Wartosc = Replace(lvarPozycja.Wartosc, ",", ".")
            lvarPozycja.Save(lvarDbHolder)
        Next

    End Sub

    Public Overloads Sub Delete(ByVal lvarDbHolder As clsDataBase)
        MyBase.Delete(lvarDbHolder, VAR_ERROR_HANDLER)
    End Sub


    Shared Function PobierzWszystkieWytopyGatunku(ByVal lvarDbHolder As clsDataBase, ByVal Zapytanie As Integer) As DataTable

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As DataTable

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_oem", "cls_material", 1, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_id", CStr(Zapytanie))
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = New DataTable
                lvarReturn.Load(lvarReader)
            End If

        End If

        Return lvarReturn

    End Function

    Shared Function PobierzSkladChemiczny(ByVal lvarDbHolder As clsDataBase, ByVal Zapytanie As Integer) As DataTable

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As DataTable

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_oem", "cls_material", 2, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_id", CStr(Zapytanie))
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = New DataTable
                lvarReturn.Load(lvarReader)
            End If

        End If

        Return lvarReturn

    End Function

    Shared Function PobierzWszystkieWytopy(ByVal lvarDbHolder As clsDataBase) As DataTable

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As DataTable

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_oem", "cls_material", 3, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = New DataTable
                lvarReturn.Load(lvarReader)
            End If

        End If

        Return lvarReturn

    End Function

#End Region

End Class
